package com.hqhy.like.front.order.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.hqhy.like.common.pojo.authentication.CurrentPrincipal;
import com.hqhy.like.common.pojo.vo.PageData;
import com.hqhy.like.common.web.JsonResult;
import com.hqhy.like.front.order.pojo.param.OrderAddNewParam;
import com.hqhy.like.front.order.pojo.param.OrderQueryParam;
import com.hqhy.like.front.order.pojo.param.OrderSearchParam;
import com.hqhy.like.front.order.pojo.param.OrderUpdateInfoParam;
import com.hqhy.like.front.order.pojo.vo.OrderStandardVO;
import com.hqhy.like.front.order.pojo.vo.OrderTableVO;
import com.hqhy.like.front.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/orders")
@Api(tags = "1.订单管理")
@Slf4j
public class OrderController {

    @Autowired
    private IOrderService orderService;

    public OrderController() {
        log.debug("创建控制器类对象：OrderController");
    }

    @PostMapping("/create")
    @ApiOperation("创建订单")
    @ApiOperationSupport(order = 100)
    public JsonResult create(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, OrderAddNewParam orderAddNewParam){
        log.debug("开始处理【创建订单】的请求，当事人: {}，新建订单参数类：{}", currentPrincipal,orderAddNewParam);
        Long orderId = orderService.create(currentPrincipal, orderAddNewParam);
            return JsonResult.ok(orderId);
    }

    @GetMapping("{id}/delete")
    @ApiOperation("删除订单")
    @ApiOperationSupport(order = 200)
    public JsonResult delete(@AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal, @PathVariable("id") Long orderId){
        log.debug("开始处理【删除订单】的请求，当事人: {}，订单id：{}", currentPrincipal,orderId);
        orderService.deleteById(orderId);
        return JsonResult.ok();
    }
    @PostMapping("/{id}/update")
    @ApiOperation("修改订单数据")
    @ApiOperationSupport(order = 300)
    public JsonResult update(@PathVariable("id") Long orderId, OrderUpdateInfoParam orderUpdateInfoParam){
        log.debug("开始处理【修改订单数据】的请求，订单id: {}，订单更新信息类：{}", orderId,orderUpdateInfoParam);
        orderService.updateInfoById(orderId,orderUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据订单id查询订单")
    @ApiOperationSupport(order = 410)
    public JsonResult getStandardById(@PathVariable("id") Long orderId){
        log.debug("开始处理【根据订单id查询订单】的请求，订单id: {}", orderId);
        OrderStandardVO order = orderService.getStandardById(orderId);
        return JsonResult.ok(order);
    }

    @PostMapping("/search/by-method-and-state")
    @ApiOperation("根据下单方式和订单状态查询订单")
    @ApiOperationSupport(order = 420)
    public JsonResult searchByMethodAndState(OrderSearchParam orderSearchParam){
        log.debug("开始处理【根据下单方式和订单状态查询订单】的请求，查询订单详情参数类: {}", orderSearchParam);
        PageData<OrderStandardVO> list = orderService.listByStateAndMethod(orderSearchParam);
        return JsonResult.ok(list);
    }

    @GetMapping("/check/by-method/{method}")
    @ApiOperation("根据下单方式查询所有确认订单页面的订单")
    @ApiOperationSupport(order = 421)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderMethod", value = "下单方式", required = true, paramType = "query", dataType = "int")
    })
    public JsonResult listAllCheckByMethod(Integer page, @PathVariable("method") Integer orderMethod){
        log.debug("开始处理【根据下单方式查询所有确认订单页面的订单】的请求，页码: {}下单方式: {}", page,orderMethod);
        Integer pageNum = page == null ? 1 : page;
        PageData<OrderStandardVO> list = orderService.listAllCheckByMethod(orderMethod, pageNum);
        return JsonResult.ok(list);
    }

    @PostMapping("/order-for-table")
    @ApiOperation("查询订单桌台数据")
    @ApiOperationSupport(order = 426)
    public JsonResult listOrderForTable(OrderQueryParam orderQueryParam){
        log.debug("开始处理【查询订单桌台数据】的请求，查询订单桌台信息参数类: {}", orderQueryParam);
        PageData<OrderTableVO> list = orderService.listOrderForTable(orderQueryParam);
        return JsonResult.ok(list);
    }

    @PostMapping("/search-by-condition")
    @ApiOperation("根据条件查询订单")
    @ApiOperationSupport(order = 423)
    public JsonResult searchByCondition(OrderSearchParam orderSearchParam){
        log.debug("开始处理【根据条件查询订单】的请求，查询订单详情参数类: {}", orderSearchParam);
        PageData<OrderStandardVO> list = orderService.searchByCondition(orderSearchParam);
        return JsonResult.ok(list);
    }

}
